package com.example.emptytest.Fragment;

import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;

import com.example.emptytest.R;

public class Fragment_2 extends Fragment implements View.OnClickListener {

    private LinearLayout content;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_2,container,false);
        content = view.findViewById(R.id.content_viewF2);
        addCard(content,view,"公告","暂无","2020-7-7");
        initView(view);
        return view;
    }

    private void initView(View view) {
    }

    @Override
    public void onClick(View v) {

    }


    private void addCard(LinearLayout LLcontentView, View v, String name, String content, String time) {
        if(v == null)
            return;
        // 开始添加卡片
        // card
        CardView cardView = new CardView(getActivity());
        LinearLayout.LayoutParams cardLayoutParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                dp(180));
        cardLayoutParams.setMargins(dp(10),dp(10),dp(10),dp(10));
        cardView.setLayoutParams(cardLayoutParams);
        cardView.setRadius(dp(8));
        cardView.setCardBackgroundColor(getContext().getResources().getColor(R.color.card_trans));
        // LinearLayout
        LinearLayout layout = new LinearLayout(getActivity());
        LinearLayout.LayoutParams LayoutParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        layout.setLayoutParams(LayoutParams);
        layout.setOrientation(LinearLayout.VERTICAL);
        // TextView
        TextView title = new TextView(getActivity());
        LinearLayout.LayoutParams titleParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        title.setLayoutParams(titleParams);
        title.setGravity(Gravity.CENTER);
        title.setText(name);
        title.setTextSize(20);
        title.setBackground(getContext().getResources().getDrawable(R.drawable.menu_press2));

        TextView contentView = new TextView(getActivity());
        LinearLayout.LayoutParams contentParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        contentParams.setMargins(dp(10),dp(10),dp(10),dp(10));
        contentView.setLayoutParams(contentParams);
        contentView.setText(content);
        contentView.setTextSize(15);

        TextView timeView = new TextView(getActivity());
        LinearLayout.LayoutParams timeParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

        timeParams.setMargins(0,dp(20),10,0);
        timeView.setLayoutParams(timeParams);
        timeView.setText(time);
        timeView.setGravity(Gravity.RIGHT);


        layout.addView(title);
        layout.addView(contentView);
        layout.addView(timeView);
        cardView.addView(layout);
        LLcontentView.addView(cardView);

    }
    // int dp 转换
    private int dp(int i) {
        return ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, i, getResources().getDisplayMetrics()));
    }

}
